package com.justcs.service;

import com.justcs.entity.*;
import com.justcs.mapper.*;
import com.justcs.view.*;
import com.mongodb.WriteResult;
import com.mongodb.client.gridfs.GridFSBucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 记录会议服务
 */
@Service
public class RecordConfInfoService {

    @Resource
    private ConferenceMapper conferenceMapper;

    @Resource
    private ConfTitleMapper confTitleMapper;

    @Resource
    private MeetingRoomMapper meetingRoomMapper;

    @Resource
    private ConfTitleRecordMapper confTitleRecordMapper;

    @Resource
    private VoiceRecordMapper voiceRecordMapper;


    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private GridFSBucket gridFSBucket;

    @Resource
    private IssueCurmeetMapper issueCurmeetMapper;



    /**
     * 查询待开始会议的详细信息
     *
     * @param workerid
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public RecordConfDetailView queryConfDetail(String workerid) {
        if (workerid != null) {
            RecordConfDetailView recordConfDetailView = new RecordConfDetailView();
            // 查询会议基本信息
            BasicConfInfoView basicConfInfoView = conferenceMapper.selectbasicconf(workerid);
            recordConfDetailView.setBasicConfInfoView(basicConfInfoView);

            // 查询会议的列席人员
            List<ListAttendersView> attendersViewsList = conferenceMapper.selectAttendsList(workerid);
            recordConfDetailView.setAttendersViews(attendersViewsList);

            // 查询会议属性
            List<ConfAttributesListView> confAttributesList = conferenceMapper.selectConfAttrList(workerid);
            recordConfDetailView.setConfattrs(confAttributesList);

            // 查询会议采集表的基本信息
            ConfCollectBasicInfoView confCollectBasicInfoView = conferenceMapper.selectCollectView(workerid);
            recordConfDetailView.setConfCollectBasicInfoView(confCollectBasicInfoView);

            // 查询上次会议所提议题
            List<ConfLastIssueView> confLastIssueViewList = conferenceMapper.selectLastIssueList(workerid);
            recordConfDetailView.setConfLastIssueViewList(confLastIssueViewList);

            // 查询本次会议需要解决的问题
            List<ConfCurIssueView> confCurIssueViewList = conferenceMapper.selectCurIssueList(workerid);
            recordConfDetailView.setConfCurIssueViewsList(confCurIssueViewList);

            // 查询会话记录(所有议题->所有会话->语音列表)
            List<ConfRecVoicDetailView> confRecVoicDetailViews = conferenceMapper.selectConfRecVoic(workerid);
            recordConfDetailView.setConfRecVoicDetailViewList(confRecVoicDetailViews);

            // 查询建议解决方案
            List<ConfSuggestionView> confSuggestionViewList = conferenceMapper.selectSuggesList(workerid);
            recordConfDetailView.setConfSuggestionViewList(confSuggestionViewList);

            return recordConfDetailView;
        }
        return null;
    }

    /**
     * 查询本次会议需要解决的问题
     * @param workerid
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ConfCurIssueView> queryconfCurIssue(String workerid) {
        return conferenceMapper.selectCurIssueList(workerid);
    }

    /**
     * 查询采集表的信息
     *
     * @param workerid
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public ModelCollectInfoView queryModelCollect(String workerid) {
        if (workerid != null) {
            ModelCollectInfoView modelCollectInfoView = new ModelCollectInfoView();
            // 查询会议基本信息
            BasicConfInfoView basicConfInfoView = conferenceMapper.selectbasicconf(workerid);
            modelCollectInfoView.setBasicConfInfoView(basicConfInfoView);

            // 会议议题搜集的基本信息
            ConfCollectBasicInfoView confCollectBasicInfoView = conferenceMapper.selectCollectView(workerid);
            modelCollectInfoView.setConfCollectBasicInfoView(confCollectBasicInfoView);

            // 查询上次会议所提议题
            List<ConfLastIssueView> confLastIssueViewList = conferenceMapper.selectLastIssueList(workerid);
            modelCollectInfoView.setConfLastIssueViewList(confLastIssueViewList);

            // 查询本次会议需要解决的问题
            List<ConfCurIssueView> confCurIssueViewList = conferenceMapper.selectCurIssueList(workerid);
            modelCollectInfoView.setConfCurIssueViewList(confCurIssueViewList);

            // 查询建议解决方案
            List<ConfSuggestionView> confSuggestionViewList = conferenceMapper.selectSuggesList(workerid);
            modelCollectInfoView.setConfSuggestionViewList(confSuggestionViewList);

            return modelCollectInfoView;
        }
        return null;
    }

    /**
     * 查询列席人员
     *
     * @param confid
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ListAttendersView> queryAttenders(String confid) {
        if (confid != null) {
            return conferenceMapper.selectAttendsList(confid);
        }
        return null;
    }


    /**
     * 添加会议议题
     *
     * @param param
     * @return
     */
    @Transactional
    public ConfTitle addConfSpeechItem(ConfTitle param) {
        if (param != null) {
            confTitleMapper.insertSelective(param);
            return param;
        }
        return null;
    }

    /**
     * 添加当前会议内容
     * @param issueCurmeet
     * @return
     */
    @Transactional
    public IssueCurmeet addIssueCurmeet(IssueCurmeet issueCurmeet) {
        if(issueCurmeet!=null) {
            issueCurmeetMapper.insertSelective(issueCurmeet);
            return issueCurmeet;
        }
        return null;
    }

    /**
     * 添加发言人发言记录
     * 1.首先查询该议题有没有被录入
     * 1a.如果被录入继续
     * 1b.如果没有被录入则录入会议议题，继续
     * 2.获取到conftitle的id
     * 3.录入会议发言记录
     * 4.返回
     *
     * @param confTitle
     * @param record
     * @return
     */
    @Transactional
    public ConfTitleRecord recordUsrSpeach(ConfTitle confTitle, ConfTitleRecord record) {
        ConfTitleRecord confTitleRecord = null;
        if (confTitle != null && record != null) {
            // 1.查询有没有录入议题
            ConfTitle savedtitle = confTitleMapper.selectContians(String.valueOf(confTitle.getConfid()),
                    confTitle.getConftitlecnt());
            if (savedtitle == null) {
                // 录入议题
                confTitleMapper.insertSelective(confTitle);
            } else {
                confTitle = savedtitle;
            }
            // 2.录入会议发言记录
            record.setConftitleid(confTitle.getId());
            confTitleRecordMapper.insertSelective(record);
            confTitleRecord = record;
        }
        return confTitleRecord;
    }


    /**
     * 保存用户的上传的文件
     *
     * @param confrecid
     * @param blobid
     * @return
     */
    @Transactional
    public VoiceRecord uploadUsrVoice(String confrecid, String blobid) {
        if (confrecid != null) {
            VoiceRecord result = new VoiceRecord();
            result.setVoiceurl(blobid);
            result.setConftitlerecordid(Integer.valueOf(confrecid));
            result.setVoicedes(String.valueOf(new Date(System.currentTimeMillis())));
            voiceRecordMapper.insertSelective(result);
            return result;
        }
        return null;
    }

    /**
     * 移除用户的记录
     *
     * @param record
     * @return
     */
    @Transactional
    public int removeUsrSpeach(ConfTitleRecord record) {
        int affected = 0;
        if (record != null) {
            // 查询该用户发言记录下面的所有录音文件
            List<VoiceRecord> voiceRecords = voiceRecordMapper.queryAllVoice(String.valueOf(record.getId()));
            // 删除mongodb中的录音文件
            int len = 0;
            if(voiceRecords!=null &&  (len = voiceRecords.size())>0) {
                String[] filesId = new String[len];
                for(int i = 0; i< len; i++) {
                    filesId[i] = voiceRecords.get(i).getVoiceurl(); // 获取其中的id
                }
                Object[] filesMongoId = filesId;
                Query query = Query.query(Criteria.where("_id").in(filesMongoId));
                gridFsTemplate.delete(query); // 批量删除mongodb文件
            }
            // 批量删除记录
            affected = confTitleRecordMapper.deleteByPrimaryKey(record.getId());
        }
        return affected;
    }

    /**
     * 删除录音，返回原始数据
     *
     * @param audioRecid
     * @return
     */
    @Transactional
    public VoiceRecord removeAudioRecord(Integer audioRecid) {
        VoiceRecord result = null;
        if(audioRecid!=null) {
            // 查询录音
            result = voiceRecordMapper.selectByPrimaryKey(audioRecid);
            if (result != null) {
                // 执行删除操作
                int flag = voiceRecordMapper.deleteByPrimaryKey(audioRecid);
                return flag > 0 ? result : null;
            }
        }
        return result;
    }


    /**
     * 修改用户发言内容
     * @param record
     * @return
     */
    @Transactional
    public int modifyUsrSpeach(ConfTitleRecord record) {
        int flag = 0;
        flag = confTitleRecordMapper.updateByPrimaryKeyWithBLOBs(record);
        return flag;
    }

    /**
     * 更新conference结论数据
     * @param conference
     * @return
     */
    @Transactional
    public int modifyConfConclusion(ConferenceWithBLOBs conference){
        int flag = 0;
        if(conference!=null){
            flag = conferenceMapper.updateByPrimaryKeySelective(conference);
        }
        return flag;
    }

}
